﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LinFu.IoC.Configuration;

namespace CoreEx.MicroMapper.Implementation
{
    [Implements(typeof(IValueConverter),LifecycleType.OncePerRequest)]
    public class ValueConverter :IValueConverter
    {
        public object Convert(object value, Type sourceAccessorType, Type targetAccessorType)
        {
            if (value == DBNull.Value)
            {
                value = null;
                return value;
            }

            if (targetAccessorType.IsValueType && value == null)
                value = Activator.CreateInstance(targetAccessorType);

            if (targetAccessorType.IsAssignableFrom(sourceAccessorType))
                return value;

            return System.Convert.ChangeType(value, targetAccessorType);            
        }
    }
}
